package com.cardmanager.pro.repository;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.cardmanager.pro.pojo.ConsumeTaskPOJO;
@Repository
public interface ConsumeTaskPOJORepository extends JpaRepository<ConsumeTaskPOJO, Long>,JpaSpecificationExecutor<ConsumeTaskPOJO>{

	@Query("select count(*) from ConsumeTaskPOJO consumeTaskPOJO where consumeTaskPOJO.userId=:userId and consumeTaskPOJO.creditCardNumber =:creditCardNumber and consumeTaskPOJO.taskStatus=:taskStatus and consumeTaskPOJO.version=:version")
	int queryTaskStatusCountAndVersion(@Param("userId")String userIdStr,@Param("creditCardNumber")String creditCardNumber,@Param("taskStatus")int taskStatus,@Param("version")String version);

	@Query("select consumeTaskPOJO from ConsumeTaskPOJO consumeTaskPOJO where consumeTaskPOJO.taskType=:taskType and consumeTaskPOJO.taskStatus=:taskStatus and consumeTaskPOJO.executeDateTime <=:nowTime and consumeTaskPOJO.executeDate=:executeDate and consumeTaskPOJO.version=:version")
	List<ConsumeTaskPOJO> findTaskTypeAndTaskStatusRepaymentTaskAndVersion(@Param("taskType")int taskType,@Param("taskStatus")int taskStatus,@Param("nowTime")String nowTime,@Param("executeDate")String executeDate,@Param("version")String version);

	@Query("select count(*) from ConsumeTaskPOJO consumeTaskPOJO where consumeTaskPOJO.userId=:userId and consumeTaskPOJO.creditCardNumber =:creditCardNumber and consumeTaskPOJO.taskStatus=:taskStatus and consumeTaskPOJO.taskType=:taskType and consumeTaskPOJO.version=:version")
	int queryTaskStatusAndTaskTypeCountAndVersion(@Param("userId")String userId,@Param("creditCardNumber") String creditCardNumber,@Param("taskStatus") int taskStatus, @Param("taskType")int taskType,@Param("version")String version);

	@Query("select count(*) from ConsumeTaskPOJO consumeTaskPOJO where consumeTaskPOJO.userId=:userId and consumeTaskPOJO.creditCardNumber =:creditCardNumber and consumeTaskPOJO.orderStatus=:orderStatus and consumeTaskPOJO.taskType=:taskType and consumeTaskPOJO.version=:version")
	int queryOrderStatusAndTaskTypeCountAndVersion(@Param("userId")String userId,@Param("creditCardNumber") String creditCardNumber, @Param("orderStatus")int orderStatus, @Param("taskType")int taskType,@Param("version")String version);

	List<ConsumeTaskPOJO> findByRepaymentTaskId(String repaymentTaskId);

	ConsumeTaskPOJO findByOrderCode(String orderCode);

	@Query("select consumeTaskPOJO from ConsumeTaskPOJO consumeTaskPOJO where consumeTaskPOJO.userId=:userId and consumeTaskPOJO.creditCardNumber =:creditCardNumber and consumeTaskPOJO.taskType=:taskType and consumeTaskPOJO.taskStatus=:taskStatus and consumeTaskPOJO.version=:version")
	ConsumeTaskPOJO findByTaskTypeAndTaskStatusConsumeTaskPOJOAndVersion(@Param("userId")String userId, @Param("creditCardNumber")String creditCardNumber, @Param("taskType")int taskType, @Param("taskStatus")int taskStatus,@Param("version")String version);

	ConsumeTaskPOJO findByConsumeTaskId(String consumeTaskId);

	@Query("select sum(consumeTaskPOJO.realAmount) from ConsumeTaskPOJO consumeTaskPOJO where consumeTaskPOJO.userId=:userId and consumeTaskPOJO.creditCardNumber =:creditCardNumber and consumeTaskPOJO.orderStatus=:orderStatus and consumeTaskPOJO.version=:version")
	BigDecimal findSumRealAmountByUserIdAndCreditCardNumberAndOrderStatusAndVersion(@Param("userId")String userId, @Param("creditCardNumber")String creditCardNumber,@Param("orderStatus")int orderStatus,@Param("version")String version);

	ConsumeTaskPOJO findByUserIdAndCreditCardNumberAndTaskTypeAndVersion(String userIdStr, String creditCardNumber, int taskType,String version);

	ConsumeTaskPOJO findByCreditCardNumberAndTaskTypeAndVersion(String creditCardNumber, int taskType,String version);

	List<ConsumeTaskPOJO> findByTaskTypeAndOrderStatusAndVersion(int taskType, int orderStatus, String version);

	List<ConsumeTaskPOJO> findByTaskTypeAndTaskStatusAndVersionAndExecuteDate(int taskType, int taskStatus,String version, String executeDate);

	ConsumeTaskPOJO findByUserIdAndCreditCardNumberAndTaskStatusAndTaskTypeAndOrderStatusAndVersion(String userId,String creditCardNumber, int taskStatus, int taskType, int orderStatus, String version);

}
